<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="服务商">
          <a-select
            v-decorator="[ 'businessServiceId', {rules: [{ required: true, message: '请选择服务商' }],initialValue: defaultBusinessService}]"
            placeholder="请选择服务商">
            <a-select-option :value="businessService.id" v-for="businessService in businessServiceList" :key="businessService.id">
              {{businessService.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"

          label="商圈名称"
        >
          <a-input placeholder="商圈名称"  :disabled="isDisabled"
                   v-decorator="[ 'name', {rules: [{ required: true, message: '请输入。。。' },{validator:this.checkContent.bind(this)}]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商圈地址"
        >
          <a-input placeholder="商圈地址"
                   v-decorator="[ 'address', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公司名称"
        >
          <a-input placeholder="公司名称"
                   v-decorator="[ 'realName', {rules: [{ required: true, message: '请输入。。。' },{validator:this.checkContent.bind(this)}]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商圈电话号码"
        >
          <a-input placeholder="商圈电话号码"
                   type="phone"    v-decorator="[ 'phone', {rules: [{ required: true, message: '请输入。。。' },{validator:this.checkPhone.bind(this)}]}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="小程序appid"
        >
          <a-input placeholder="小程序appid"
                   v-decorator="[ 'miniAppid', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="小程序secret"
        >
          <a-input placeholder="小程序secret"
                   v-decorator="[ 'miniSecret', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { save } from '@/api/companyService/company'
  import { selectAll } from '@/api/businessService/business'
  import pick from 'lodash.pick'

  export default {
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),
        businessServiceList:[],
        defaultBusinessService:[],
        isDisabled: false,
        businessService:{}
      }
    },
    //--############  data() ###############-->

    created() {
      selectAll().then(res => {
        this.businessServiceList = res.result
        this.defaultBusinessService = this.businessServiceList[0].id
        this.businessService = {
          'businessServiceId': this.defaultBusinessService
        }

      })
    },

    //--############  methods() ###############-->
    methods: {
      add() {
        // this.isDisabled = false
        this.visible = true
      },
      edit(record) {
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'businessServiceId','name', 'address', 'realName','phone','miniAppid','miniSecret'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl={}
      },
      checkContent(rule,value,callback){
        const reg = /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/;
        if(!reg.test(value)){
          callback('仅允许输入汉字、字母、数字和下划线');
          return;
        }else  if(value && value.length>80){
          callback('文字内容多于80字');
          return;
        }
        callback()
      },
      checkPhone(rule,value,callback){
        if(!(/^1(3|4|5|7|8)\d{9}$/.test(value))){
          callback('手机号有误，请重填');
        }else {
          callback();
        }
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id;
            _this.confirmLoading = true
            save(values).then((res) => {
              console.log('res',res)
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              }else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
